package misc

// import scala.util.parsing.combinator.lexical.StdLexical
// import scala.util.parsing.combinator.syntactical._
import lexpar.StandardTokenParsers
object OrderDSL extends StandardTokenParsers {
  // lexical.delimiters ++= List("(", ")", ",")
  // lexical.reserved += ("buy", "sell", "shares", "at", "max", "min", "for", "trading", "account")
  // Add token declarations / definitions
  literal("(", ")", ",")
  literal("buy", "sell", "shares", "at", "max", "min", "for", "trading", "account")

  def instr = trans ~ account_spec
  def trans = "(" ~> repsep(trans_spec, ",") <~ ")"
  def trans_spec = buy_sell ~ buy_sell_instr
  def account_spec = "for" ~> "trading" ~> "account" ~> stringLit
  def buy_sell = ("buy" | "sell")
  def buy_sell_instr = security_spec ~ price_spec
  def security_spec = numericLit ~ ident ~ "shares"
  def price_spec = "at" ~ ("min" | "max") ~ numericLit
  
  def main(args: Array[String]) {
    val input = """ (buy 100 IBM shares at max 45, 
      sell 50 CISCO shares at min 25, 
      buy 100 Google shares at max 800) 
      for trading account "SSS1234" """
    println(parseAll(instr, input))
  }
}